<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="姓名" prop="stuName">
        <el-input v-model="queryParams.stuName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="stuSex">
        <el-select v-model="queryParams.stuSex" placeholder="请选择性别" clearable>
          <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="学号" prop="stuCode">
        <el-input v-model="queryParams.stuCode" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="登录手机号" prop="tel">
        <el-input v-model="queryParams.tel" placeholder="请输入登录手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="生日" prop="birthDay">
        <el-date-picker clearable v-model="queryParams.birthDay" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="校区" prop="schoolName">
        <el-input v-model="queryParams.schoolName" readonly="readonly" @click.native="openSelectSchool(1)"
          placeholder="请输入校区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="省、市、区" prop="queryProvinceAndCityAndDistrict">
        <el-cascader placeholder="请选择省、市、区" v-model="queryParams.queryProvinceAndCityAndDistrict" :options="options"
          @keyup.enter.native="handleQuery" @lazy-load="selectLoadOptions"
          :props="{ lazy: true,checkStrictly:true,multiple: true, lazyLoad: selectLoadOptions,value:'regionId',label:'regionName' }"
          clearable></el-cascader>
      </el-form-item>

      <el-form-item label="上线" prop="state">
        <el-select v-model="queryParams.state" clearable>
          <el-option v-for="dict in dict.type.common_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['school:classes:edit']">添加学生</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['school:classes:edit']">批量移除学生</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="姓名" fixed align="center" prop="stuName" :show-overflow-tooltip="true"/>
      <el-table-column label="头像" align="center" prop="avatarURL">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatarURL" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="stuSex" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.stuSex" />
        </template>
      </el-table-column>
      <el-table-column label="学号" align="center" prop="stuCode" :show-overflow-tooltip="true"/>
      <el-table-column label="登录手机号" align="center" prop="tel" :show-overflow-tooltip="true"/>
      <el-table-column label="校区" align="center" prop="schoolName" :show-overflow-tooltip="true"/>
      <el-table-column label="省、市、区" align="center" prop="provinceAndCityAndDistrict" :show-overflow-tooltip="true"/>
      <el-table-column label="上线" align="center" prop="state" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="gmtCreate" :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['school:classes:edit']">移除学生</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 选择校区 -->
    <SelectSchool ref="selectSchool" :open="selectFlag"
      @selected-data="selectSchoolData" @close-select-window="closeSelectSchool" />

    <SelectStudent ref="select" :id="queryParams.includeClassId" @ok="handleQuery" />
  </div>
</template>

<script>
  import {
    listStudent,
    manStudent
  } from "@/api/bus_web/school/student";
  import {
    loadOptions
  } from "@/api/system/sys_region";
  import {
    mapState
  } from 'vuex';
  import store from "@/store";
  import SelectSchool from "../schoolinfo/select.vue"
  import SelectStudent from "./selectStudent";
  export default {
    name: "Handle-Student",
    dicts: ['sys_user_sex', 'common_state'],
    components: {
      SelectSchool,
      SelectStudent
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 是否显示校区选择
        selectFlag: false,
        options: [],
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 学生信息表格数据
        studentList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          stuName: null,
          avatar: null,
          stuSex: null,
          stuCode: null,
          tel: null,
          birthDay: null,
          schoolId: null,
          schoolName: null,
          schoolIdArray: [],
          province: null,
          city: null,
          district: null,
          address: null,
          state: null,
          includeClassId: undefined,
          queryProvinceAndCityAndDistrict: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {

        }
      };
    },
    computed: {
      ...mapState({
        school: state => state.selectSchool.school,
        schoolSelectModel: state => state.selectSchool.schoolSelectModel,
      })
    },
    created() {
      const id = this.$route.params && this.$route.params.id;
      if (id) {
        this.queryParams.includeClassId = id;
        this.getList();
      }
    },
    methods: {
      /**省 市 区*/
      selectLoadOptions(node, resolve) {
        var query = {
          regionLevel: node.level + 1,
          parentId: node.value || null
        }
        loadOptions(query).then(response => {
          resolve(response.data);
        });
      },
      /** 查询学生信息列表 */
      getList() {
        this.loading = true;
        const id = this.$route.params && this.$route.params.id;
        this.queryParams.includeClassId = id;
        listStudent(this.queryParams).then(response => {
          this.studentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.reset();
      },
      // 表单重置
      reset() {
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      handleDelete(row) {
        const ids = row.id || this.ids;
        var stuIdList = new Array()
        if (!Array.isArray(ids)) {
          stuIdList.push(ids)
        } else {
          stuIdList = ids
        }
        const _data = {
          id: this.queryParams.includeClassId,
          op: 2,
          stuIdList: stuIdList
        }
        this.$modal.confirm('是否确认删除班级学生编号为"' + ids + '"的数据项？').then(function() {
          return manStudent(_data);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.$refs.select.show();
      },
      //选择校区
      openSelectSchool(selectVal) {
        this.selectFlag = true
        // selectVal =1 查询模式 =2 创建、修改模式
        this.$store.dispatch('selectSchool/cleanSchool')
        this.$store.dispatch('selectSchool/setSchoolSelectModel', selectVal)
      },
      closeSelectSchool() {
        this.selectFlag = false
      },
      //确定校区
      selectSchoolData() {
        this.closeSelectSchool()
        if (this.schoolSelectModel == 2) {
          this.form.schoolName = this.school[0].schoolName
          this.form.schoolId = this.school[0].id
          this.$refs.form.clearValidate(['schoolName']);
        } else {
          this.queryParams.schoolName = this.school.map(item => item.schoolName).join(',')
          this.queryParams.schoolIdArray = this.school.map(item => item.id)
        }
        this.$store.dispatch('selectSchool/cleanSchool')
      }
    }
  };
</script>
<style scoped>
  .uploadImage {
    width: 100%;
    height: 100%;
  }
</style>
